package com.xsoft.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView displayWidth;
    private TextView displayHeight;
    private TextView displayDensity;
    private EditText dp2pxEdt;
    private EditText px2dpEdt;
    private EditText sp2pxEdt;
    private EditText px2spEdt;
    private TextView dp2pxTxt;
    private TextView px2dpTxt;
    private TextView sp2pxTxt;
    private TextView px2spTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        displayWidth = findViewById(R.id.display_width);
        displayWidth.setText("屏幕宽度为：" + DisplayUtil.getScreenWidth(this));
        displayHeight = findViewById(R.id.display_height);
        displayHeight.setText("屏幕高度为：" + DisplayUtil.getScreenHeight(this));
        displayDensity = findViewById(R.id.display_density);
        displayDensity.setText("屏幕密度为：" + DisplayUtil.getScreenDensity(this));
        dp2pxEdt = findViewById(R.id.dp2px_edt);
        findViewById(R.id.dp2px_btn).setOnClickListener(this);
        dp2pxTxt = findViewById(R.id.dp2px_txt);
        px2dpEdt = findViewById(R.id.px2dp_edt);
        findViewById(R.id.px2dp_btn).setOnClickListener(this);
        px2dpTxt = findViewById(R.id.px2dp_txt);
        sp2pxEdt = findViewById(R.id.sp2px_edt);
        findViewById(R.id.sp2px_btn).setOnClickListener(this);
        sp2pxTxt = findViewById(R.id.sp2px_txt);
        px2spEdt = findViewById(R.id.px2sp_edt);
        findViewById(R.id.px2sp_btn).setOnClickListener(this);
        px2spTxt = findViewById(R.id.px2sp_txt);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dp2px_btn:
                String dp2px = dp2pxEdt.getText().toString().trim();
                if (!TextUtils.isEmpty(dp2px)) {
                    dp2pxTxt.setText(String.valueOf(UnitUtil.dp2px(this, Float.valueOf(dp2px))));
                }
                break;
            case R.id.px2dp_btn:
                String px2dp = px2dpEdt.getText().toString().trim();
                if (!TextUtils.isEmpty(px2dp)) {
                    px2dpTxt.setText(String.valueOf(UnitUtil.px2dp(this, Float.valueOf(px2dp))));
                }
                break;
            case R.id.sp2px_btn:
                String sp2px = sp2pxEdt.getText().toString().trim();
                if (!TextUtils.isEmpty(sp2px)) {
                    sp2pxTxt.setText(String.valueOf(UnitUtil.sp2px(this, Float.valueOf(sp2px))));
                }
                break;
            case R.id.px2sp_btn:
                String px2sp = px2spEdt.getText().toString().trim();
                if (!TextUtils.isEmpty(px2sp)) {
                    px2spTxt.setText(String.valueOf(UnitUtil.px2sp(this, Float.valueOf(px2sp))));
                }
                break;
        }
    }
}